package com.ktjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pages.ProjectPage;
import com.ktjy.pojo.BasDict;
import com.ktjy.pojo.CstService;
import com.ktjy.pojo.SysUser;
import com.ktjy.pojo.vo.SysUserVo;
import com.ktjy.service.BasDictService;
import com.ktjy.service.CstCustomerService;
import com.ktjy.service.CstServiceService;
import com.ktjy.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.auth.Subject;
import java.util.HashMap;
import java.util.Map;

@Controller
public class CstServiceController {
    @Autowired
    CstServiceService cstServiceService;
    @Autowired
    CstCustomerService cstCustomerService;
    @Autowired
    BasDictService basDictService;
    @Autowired
    SysUserService sysUserService;

    @RequestMapping("/service/list")
    public Object CstServiceLists(CstService cstService, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "2") Integer pageSize, Model model) {
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.hasText(cstService.getSvrCustName()), CstService::getSvrCustName, cstService.getSvrCustName());
        queryWrapper.lambda().like(StringUtils.hasText(cstService.getSvrStatus()), CstService::getSvrStatus, cstService.getSvrStatus());
        queryWrapper.lambda().like(StringUtils.hasText(cstService.getSvrResult()), CstService::getSvrResult, cstService.getSvrResult());
        IPage<CstService> cstServiceIPage = new Page<>(pageIndex, pageSize);
        Page<CstService> page = (Page<CstService>) cstServiceService.page(cstServiceIPage, queryWrapper);
        ProjectPage<CstService> projectPage = new ProjectPage<>();
        BeanUtils.copyProperties(page, projectPage);
        model.addAttribute("planPage", projectPage);
        model.addAttribute("svrCustName", cstService.getSvrCustName());
        model.addAttribute("svrStatus", cstService.getSvrStatus());
        model.addAttribute("svrResult", cstService.getSvrResult());
        return "/service/list";
    }

    @RequestMapping("/service/deal")
    public Object CstServiceDeal(CstService cstService, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "2") Integer pageSize, Model model) {
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.hasText(cstService.getSvrCustName()), CstService::getSvrCustName, cstService.getSvrCustName());
        queryWrapper.lambda().like(StringUtils.hasText(cstService.getSvrStatus()), CstService::getSvrStatus, cstService.getSvrStatus());
        queryWrapper.lambda().like(StringUtils.hasText(cstService.getSvrResult()), CstService::getSvrResult, cstService.getSvrResult());
        IPage<CstService> cstServiceIPage = new Page<>(pageIndex, pageSize);
        Page<CstService> page = (Page<CstService>) cstServiceService.page(cstServiceIPage, queryWrapper);
        ProjectPage<CstService> projectPage = new ProjectPage<>();
        BeanUtils.copyProperties(page, projectPage);
        model.addAttribute("page", projectPage);
        model.addAttribute("svrCustName", cstService.getSvrCustName());
        model.addAttribute("svrStatus", cstService.getSvrStatus());
        model.addAttribute("svrResult", cstService.getSvrResult());
        QueryWrapper<BasDict> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(BasDict::getDictType, "服务类型");
        model.addAttribute("dicts", basDictService.list(queryWrapper1));
        return "/service/deal";
    }

    @RequestMapping("/service/add")
    public Object CstServiceAdd(Model model) {
        //查询客户
        model.addAttribute("customers", cstCustomerService.list());
        //查询服务列表
        QueryWrapper<BasDict> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BasDict::getDictType, "服务类型");
        model.addAttribute("dicts", basDictService.list(queryWrapper));
        return "/service/add";
    }

    /**
     * 处理
     * @param svrId
     * @param model
     * @return
     */
    @RequestMapping("service/edit")
    public Object CstServiceAdd(Long svrId, Model model) {

        model.addAttribute("serviced", cstServiceService.getById(svrId));
        return "/service/dealEdit";
    }

    @RequestMapping("/service/allot/{svrId}/{svrDueId}")
    @ResponseBody
    public Object CstServiceAllot(@PathVariable Long svrId, @PathVariable Long svrDueId, CstService cstService) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(svrId != null, CstService::getSvrId, svrId);
        cstService.setSvrStatus("已分配");
        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();
        cstService.setSvrDueId(svrDueId);
        SysUserVo sysUserVo1 = sysUserService.getById(svrDueId);
        cstService.setSvrDueTo(sysUserVo.getUsrName());
        cstService.setSvrDealBy(sysUserVo1.getUsrName());
        cstService.setSvrDealId(sysUserVo1.getUsrId());
        boolean flg = cstServiceService.update(cstService, queryWrapper);
        map.put("editResult", flg);
        return map;
    }

    @RequestMapping("/service/dispatch")
    public Object CstServiceDispatch(CstService cstService, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "2") Integer pageSize, Model model) {
        QueryWrapper<CstService> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(StringUtils.hasText(cstService.getSvrCustName()), CstService::getSvrCustName, cstService.getSvrCustName());
        queryWrapper.lambda().like(StringUtils.hasText(cstService.getSvrTitle()), CstService::getSvrTitle, cstService.getSvrTitle());
        queryWrapper.lambda().like(StringUtils.hasText(cstService.getSvrType()), CstService::getSvrType, cstService.getSvrType());
        IPage<CstService> cstServiceIPage = new Page<>(pageIndex, pageSize);
        Page<CstService> page = (Page<CstService>) cstServiceService.page(cstServiceIPage, queryWrapper);
        ProjectPage<CstService> projectPage = new ProjectPage<>();
        BeanUtils.copyProperties(page, projectPage);
        //查询服务类型
        QueryWrapper<BasDict> queryWrapperq = new QueryWrapper<>();
        queryWrapperq.lambda().eq(BasDict::getDictType, "服务类型");
        model.addAttribute("dicts", basDictService.list(queryWrapperq));
        model.addAttribute("svrType", basDictService.list(queryWrapperq));
        //查询用户
        QueryWrapper<SysUserVo> sysUserVoQueryWrapper = new QueryWrapper<>();
        sysUserVoQueryWrapper.lambda().in(SysUserVo::getUsrRoleId, 2, 33);
        model.addAttribute("users", sysUserService.list(sysUserVoQueryWrapper));
        model.addAttribute("servicePage", projectPage);
        model.addAttribute("svrCustName", cstService.getSvrCustName());
        model.addAttribute("svrTitle", cstService.getSvrTitle());
        return "/service/dispatch";
    }

    @RequestMapping("/service/save")
    public Object CstServiceSave(CstService cstService, Model model) {
        boolean flg = cstServiceService.saveOrUpdate(cstService);
        if (flg) {
            return "redirect:/service/list";
        }
        return "/service/add";
    }
}
